/*
  HvacReset
  Part of the HVAC series of libraries for the Arduino.  
  Calculates direct/reverse reset ouput based on input.
 
  This example code is in the public domain.
  
  Matt Dobey
  3/13/2013
  version: 1_0
*/

#include "Arduino.h"
#include "HvacReset.h"

//  Constructs the class "HvacReset"
HvacReset::HvacReset(byte action) {
  _action = action;
}

//  Public function called to calculate the reset output
double HvacReset::reset(double x2, double x1, double x3, double y1, double y3) {
  if ((_action == 2) || ((_action == 0) && (y1 < y3))) {
    _directReset(x1, x2, x3, y1, y3);
  }
  else {
    _reverseReset(x1, x2, x3, y1, y3);
  }  
}  

//  Private function that calculates a direct reset setpoint
double HvacReset::_directReset(double x1, double x2, double x3, double y1, double y3) {
  //  Direct Reset
  outVal = (y1 + ((x2 - x1) * (y3 - y1)) / (x3 - x1));
 
  //  Limit output to hi / low outputs
  if (outVal >= y3) {
    outVal = y3;
  }  
  else if (outVal <= y1) {
    outVal = y1;
  }
  //  Serial.print("Direct Reset outVal : ");  Serial.println(outVal);  //  Print outVal to serial  
  return outVal;
  //  End of Direct reset...
}  


//  Private function that calculates a reverse reset setpoint
double HvacReset::_reverseReset(double x1, double x2, double x3, double y1, double y3) {
  //  Reverse Reset
  outVal = (y1 + ((x2 - x1) * (y3 - y1)) / (x3 - x1));
  
  //  Limit output to hi / low outputs
  if (outVal >= y1) {
    outVal = y1;
  }  
  else if (outVal <= y3) {
    outVal = y3;
  }
  //  Serial.print("Reverse Reset outVal : ");  Serial.println(outVal);  //  Print outVal to serial
  return outVal;
  //  End of Reverse reset...
}  



